﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Medianamik.Core.Logging
{
    public abstract class TableBuilder
    {
        protected abstract void Build(XElement table);

        public XElement Build()
        {
            XElement table = new XElement("table",
                new XAttribute("border", 1),
                new XAttribute("cellpadding", 0),
                new XAttribute("cellspacing", 0),
                new XAttribute("style", "font-family:Verdana;font-size:70%;width:960px;"));

            Build(table);
            return table;

        }

        protected virtual XElement BuildRow(string pKey, string pValue, bool evenRow)
        {
            return BuildRow(pKey, new XText(pValue), evenRow );
        }
        protected virtual XElement BuildRow(string pKey, XNode pValue, bool evenRow)
        {
            string style = "padding:0.4em;";
            if (evenRow) style += "background-color:rgb(233,233,233);";
            
            var row = new XElement("tr",
                new XElement("td"
                , new XAttribute("style",style) 
            , new XElement("strong", new XText(pKey))));

            row.Add(new XElement("td"
                , new XAttribute("style", style) 
            , pValue));

            return row;
        }

        protected virtual XElement BuildHeaderRow(string pValue, int pColspan)
        {
            XElement header = new XElement("tr"
                    , new XElement("th"
                        , new XAttribute("style", "padding:0.4em;") 
                        , new XAttribute("colspan", pColspan)
                        , new XText(pValue)));

            return header;
        }
    }
}
